package com.flyco.modules.pms.service.impl;

import com.flyco.modules.pms.entity.PmsProductAttributeCategory;
import com.flyco.modules.pms.entity.PmsProductAttribute;
import com.flyco.modules.pms.mapper.PmsProductAttributeMapper;
import com.flyco.modules.pms.mapper.PmsProductAttributeCategoryMapper;
import com.flyco.modules.pms.service.IPmsProductAttributeCategoryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 类型
 * @Author: flyco
 * @Date:   2019-11-18
 * @Version: V1.0
 */
@Service
public class PmsProductAttributeCategoryServiceImpl extends ServiceImpl<PmsProductAttributeCategoryMapper, PmsProductAttributeCategory> implements IPmsProductAttributeCategoryService {

	@Autowired
	private PmsProductAttributeCategoryMapper pmsProductAttributeCategoryMapper;
	@Autowired
	private PmsProductAttributeMapper pmsProductAttributeMapper;
	
	@Override
	@Transactional
	public void saveMain(PmsProductAttributeCategory pmsProductAttributeCategory, List<PmsProductAttribute> pmsProductAttributeList) {
		pmsProductAttributeCategoryMapper.insert(pmsProductAttributeCategory);
		for(PmsProductAttribute entity:pmsProductAttributeList) {
			//外键设置
			entity.setProductAttributeCategoryId(pmsProductAttributeCategory.getId());
			pmsProductAttributeMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(PmsProductAttributeCategory pmsProductAttributeCategory,List<PmsProductAttribute> pmsProductAttributeList) {
		pmsProductAttributeCategoryMapper.updateById(pmsProductAttributeCategory);
		
		//1.先删除子表数据
		pmsProductAttributeMapper.deleteByMainId(pmsProductAttributeCategory.getId());
		
		//2.子表数据重新插入
		for(PmsProductAttribute entity:pmsProductAttributeList) {
			//外键设置
			entity.setProductAttributeCategoryId(pmsProductAttributeCategory.getId());
			pmsProductAttributeMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		pmsProductAttributeMapper.deleteByMainId(id);
		pmsProductAttributeCategoryMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			pmsProductAttributeMapper.deleteByMainId((Long)id);
			pmsProductAttributeCategoryMapper.deleteById(id);
		}
	}
	
}
